/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Param;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <dl>
 * <dd>描述: ~ ReportController</dd>
 * <dd>创建时间：  10:51 2021/3/8</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/3/8      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("report")
public class ReportController {

    @DubboReference
    private ReportService reportService;

/**
 * @Author: 11341
 * @Date: 9:23 2021/3/13
 * @Parms []
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 会员年龄对比
*/
    @GetMapping("getMemberAge")
    public Result getMemberAge() {
        Map<String,Object> map=reportService.getMemberAge();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    /**
     * @Author: 11341
     * @Date: 9:21 2021/3/13
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 会员性别对比
    */
    @GetMapping("getMemberSex")
    public Result getMemberSex() {
        Map<String, Object> resultMap = reportService.getMemberSex();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
    }



    /**
     * @Author: guodong
     * @Date: 10:52 2021/3/8
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 获取会员统计数据
    */
    @GetMapping("getMemberReport")
    public Result getMemberReport(){
        Map<String, Object> resultMap = new HashMap<>();
        //组装最近12个月的日期数据
        List<String> months = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -13);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            String month = sdf.format(calendar.getTime());
            months.add(month);
        }
        resultMap.put("months", months);
        //查询最近12个月的会员统计数据
        List<Integer> memberCount = reportService.getMemberReport(months);
        resultMap.put("memberCount", memberCount);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
    }

    /**
     * @Author: guodong
     * @Date: 8:58 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询套餐统计数据
    */
    @GetMapping("getSetmealReport")
    public Result getSetmealReport(){
        Map<String, Object> resultMap = reportService.getSetmealReport();
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,resultMap);
    }

    /**
     * @Author: guodong
     * @Date: 9:42 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 会员统计
    */
    @GetMapping("getBusinessReportData")
    public Result getBusinessReportData(){
        Map<String, Object> resultMap = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, resultMap);
    }

    /**
     * 导出运营数据表格
     * @param response
     * @throws IOException
     */
    @GetMapping("exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response) throws IOException {
        Map<String, Object> resultMap = reportService.getBusinessReportData();
        String todayString = (String) resultMap.get("reportDate");
        Integer todayNewMember = (Integer) resultMap.get("todayNewMember");
        Integer totalMember = (Integer) resultMap.get("totalMember");
        Integer thisWeekNewMember = (Integer) resultMap.get("thisWeekNewMember");
        Integer thisMonthNewMember = (Integer) resultMap.get("thisMonthNewMember");
        Integer todayOrderNumber = (Integer) resultMap.get("todayOrderNumber");
        Integer todayVisitsNumber = (Integer) resultMap.get("todayVisitsNumber");
        Integer thisWeekOrderNumber = (Integer) resultMap.get("thisWeekOrderNumber");
        Integer thisWeekVisitsNumber = (Integer) resultMap.get("thisWeekVisitsNumber");
        Integer thisMonthOrderNumber = (Integer) resultMap.get("thisMonthOrderNumber");
        Integer thisMonthVisitsNumber = (Integer) resultMap.get("thisMonthVisitsNumber");
        List<Map> hotSetmeal = (List<Map>) resultMap.get("hotSetmeal");


        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        OutputStream outputStream = response.getOutputStream();

        XSSFSheet sheet = workbook.getSheetAt(0);

        sheet.getRow(2).getCell(5).setCellValue(todayString);
        sheet.getRow(4).getCell(5).setCellValue(todayNewMember);
        sheet.getRow(4).getCell(7).setCellValue(totalMember);
        sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
        sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
        sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);
        sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
        sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
        sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
        sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
        sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);

        int rowNum = 12;
        for (Map map : hotSetmeal) {
            XSSFRow row = sheet.getRow(rowNum);
            row.getCell(4).setCellValue((String) map.get("name"));
            row.getCell(5).setCellValue((Long) map.get("setmeal_count"));
            row.getCell(6).setCellValue(String.valueOf((BigDecimal) map.get("proportion")));
            rowNum++;
        }

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition", "attachment;fileName=" + todayString + "_report.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
        inputStream.close();
    }

    /**
     * @Author: Lenovo
     * @Date: 10:32 2021-3-11
     * @Parms []
     * @ReturnType: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Description: 会员统计
     */
    @GetMapping("getMemberReportMonth")
    public Result getMemberReportMonth(Param param) throws ParseException {
        System.out.println(123);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date parse = simpleDateFormat.parse(param.getStarttime());
        Date parse1 = simpleDateFormat.parse(param.getEndtime());
        String format = simpleDateFormat.format(parse);
        String format1 = simpleDateFormat.format(parse1);
        param.setStarttime(format);
        param.setEndtime(format1);
        long time = parse.getTime();
        long time1 = parse1.getTime();
        long second = time1 - time;
        long sj = second / 1000 / 60 / 60 / 24 / 30;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        Map<String, Object> resultMap = new HashMap<>();
        //组装最近12个月的日期数据
        List<String> months = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, (int) -sj + 1);
        for (long i = 0; i < sj; i++) {
            calendar.add(Calendar.MONTH, 1);
            String month = sdf.format(calendar.getTime());
            months.add(month);
        }
        resultMap.put("months", months);
        //查询最近12个月的会员统计数据
        List<Integer> memberCount = reportService.getMemberDate(months);
        resultMap.put("memberCount", memberCount);
        return new Result(true, "查询成功", resultMap);

    }

    }

